package com.martin.dms.model.hib;

import java.util.HashSet;
import java.util.Set;

import com.martin.dms.model.Delivery;
import com.martin.dms.model.Item;
import com.martin.dms.model.Supplier;

/**
 * Hibernate supplier implementation.
 * 
 * @author mtsvetkov
 */
public class SupplierHib implements Supplier {

    private Long id;

    private String name;
    private String address;
    private String email;
    private String phoneNumber;

    private Set<Item> items = new HashSet<Item>();
    private Set<Delivery> deliveries = new HashSet<Delivery>();

    public SupplierHib() {
    }

    /**
     * {@inheritDoc}
     */
    public Long getId() {
        return id;
    }

    /**
     * {@inheritDoc}
     */
    public String getAddress() {
        return address;
    }

    /**
     * Sets the address of the supplier.
     * 
     * @param address - the address of the supplier.
     */
    public void setAddress(String address) {
        this.address = address;
    }

    /**
     * {@inheritDoc}
     */
    public Set<Delivery> getDeliveries() {
        return deliveries;
    }

    /**
     * Sets the history of all deliveries made by the supplier.
     * 
     * @param deliveries - history.
     */
    public void setDeliveries(Set<Delivery> deliveries) {
        this.deliveries = deliveries;
    }

    /**
     * Adds a delivery for this supplier.
     * 
     * @param delivery to add.
     */
    public void addDelivery(DeliveryHib delivery) {
        deliveries.add(delivery);
        delivery.setSupplier(this);
    }

    /**
     * {@inheritDoc}
     */
    public String getEmail() {
        return email;
    }

    /**
     * Sets the email of the supplier.
     * 
     * @param email - the email of the supplier.
     */
    public void setEmail(String email) {
        this.email = email;
    }

    /**
     * {@inheritDoc}
     */
    public Set<Item> getItems() {
        return items;
    }

    /**
     * Sets the set of items that the supplier can deliver.
     * 
     * @param items - the set of items that the supplier can deliver.
     */
    public void setItems(Set<Item> items) {
        this.items = items;
    }

    /**
     * Adds a new item to the supplier.
     * 
     * @param item to add.
     */
    public void addItem(ItemHib item) {
        items.add(item);
    }

    /**
     * {@inheritDoc}
     */
    public String getName() {
        return name;
    }

    /**
     * Sets the name of the supplier.
     * 
     * @param name - the name of the supplier.
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * {@inheritDoc}
     */
    public String getPhoneNumber() {
        return phoneNumber;
    }

    /**
     * Sets the phone number of the supplier.
     * 
     * @param phoneNumber - the phone number of the supplier.
     */
    public void setPhoneNumber(String phoneNumber) {
        this.phoneNumber = phoneNumber;
    }

    /**
     * {@inheritDoc}
     */
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (!(obj instanceof SupplierHib)) {
            return false;
        }
        final SupplierHib supplier = (SupplierHib) obj;
        if (supplier.getId() != null && this.getId() != null) {
            return supplier.getId().equals(this.getId());
        }
        return false;
    }

    /**
     * {@inheritDoc}
     */
    public int hashCode() {
        int result = 17;
        if (getId() != null) {
            result += 37 * result + getId().hashCode();
        }
        return result;
    }
}
